﻿using System; 
using System.Text;
using System.Collections.Generic; 
using System.Data;
using System.Data.SqlClient;
using ART.Data;
using ART.Common;
using ART.Model;
using ART.Cache;

namespace ART.BLL {
  //ExamQuestion
 	public partial class B_ExamQuestion
	{
   		
		public B_ExamQuestion()
		{}
        #region __________基础方法__________
		/// <summary>
		/// 删除指定缓存
		/// </summary>
		public void ClearCache(string key)
		{
		    string CacheKey = "BLL/ExamQuestion";
			if(!string.IsNullOrEmpty(key)){
			    CacheKey+="/"+key;
			}
			LRSCache.GetCacheService().RemoveObject(CacheKey);
		}
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void  Add(ART.Model.M_ExamQuestion model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into ExamQuestion(");			
            strSql.Append("Id,Title,TypeId,QuestionContent,Answer,CreateTime");
			strSql.Append(") values (");
            strSql.Append("@Id,@Title,@TypeId,@QuestionContent,@Answer,@CreateTime");            
            strSql.Append(") ");            
            	
			#region __________参数__________	
			SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@Title", SqlDbType.NVarChar,250) ,            
                        new SqlParameter("@TypeId", SqlDbType.Int,4) ,            
                        new SqlParameter("@QuestionContent", SqlDbType.Text) ,            
                        new SqlParameter("@Answer", SqlDbType.Text) ,            
                        new SqlParameter("@CreateTime", SqlDbType.DateTime)             
              
            };
			
			            
            parameters[0].Value = model.Id;                        
            parameters[1].Value = model.Title;                        
            parameters[2].Value = model.TypeId;                        
            parameters[3].Value = model.QuestionContent;                        
            parameters[4].Value = model.Answer;                        
            parameters[5].Value = model.CreateTime;            			
            #endregion
			            SqlHelper.ExecNonQuery(strSql.ToString(),parameters);
            		
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ART.Model.M_ExamQuestion model)
		{
		    StringBuilder strSql=new StringBuilder();
			#region __________语句__________
			strSql.Append("update ExamQuestion set ");
			                        
            strSql.Append(" Id = @Id , ");                                    
            strSql.Append(" Title = @Title , ");                                    
            strSql.Append(" TypeId = @TypeId , ");                                    
            strSql.Append(" QuestionContent = @QuestionContent , ");                                    
            strSql.Append(" Answer = @Answer , ");                                    
            strSql.Append(" CreateTime = @CreateTime  ");            			
			strSql.Append(" where Id=@Id  ");
			
			#endregion	
			
			#region __________参数__________	
            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@Title", SqlDbType.NVarChar,250) ,            
                        new SqlParameter("@TypeId", SqlDbType.Int,4) ,            
                        new SqlParameter("@QuestionContent", SqlDbType.Text) ,            
                        new SqlParameter("@Answer", SqlDbType.Text) ,            
                        new SqlParameter("@CreateTime", SqlDbType.DateTime)             
              
            };
            			            
            parameters[0].Value = model.Id;                        
            parameters[1].Value = model.Title;                        
            parameters[2].Value = model.TypeId;                        
            parameters[3].Value = model.QuestionContent;                        
            parameters[4].Value = model.Answer;                        
            parameters[5].Value = model.CreateTime;            			
            #endregion
			
            int rows=SqlHelper.ExecNonQuery(strSql.ToString(),parameters);
			if (rows > 0)
			{
			//string cachekey="info_"+Id;
			//ClearCache(cachekey);
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ExamQuestion ");
			strSql.Append(" where Id=@Id ");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)			};
			parameters[0].Value = Id;

			int rows=SqlHelper.ExecNonQuery(strSql.ToString(),parameters);
			
			//string cachekey="info_"+Id;
			//ClearCache(cachekey);
			
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ART.Model.M_ExamQuestion GetModel(int Id)
		{
			//string CacheKey = "BLL/ExamQuestion/info_" + Id;
			//object objModel = LRSCache.GetCacheService().RetrieveObject(CacheKey);
			// if (objModel == null)
			 //{
				StringBuilder strSql=new StringBuilder();
				strSql.Append("select * from ExamQuestion ");
				strSql.Append(" where Id=@Id ");
				
							SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)			};
			parameters[0].Value = Id;

				
				ART.Model.M_ExamQuestion model=new ART.Model.M_ExamQuestion();
				DataSet ds=SqlHelper.ExecDataSet(strSql.ToString(),parameters);
				
				if(ds.Tables[0].Rows.Count>0)
				{
				    #region __________实体赋值__________
													if(ds.Tables[0].Rows[0]["Id"].ToString()!="")
				{
					model.Id=int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
				}
																																				model.Title= ds.Tables[0].Rows[0]["Title"].ToString();
																												if(ds.Tables[0].Rows[0]["TypeId"].ToString()!="")
				{
					model.TypeId=int.Parse(ds.Tables[0].Rows[0]["TypeId"].ToString());
				}
																																				model.QuestionContent= ds.Tables[0].Rows[0]["QuestionContent"].ToString();
																																model.Answer= ds.Tables[0].Rows[0]["Answer"].ToString();
																												if(ds.Tables[0].Rows[0]["CreateTime"].ToString()!="")
				{
					model.CreateTime=DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
				}
																									
						
					#endregion
					
					//LRSCache.GetCacheService().AddObject(CacheKey, objModel, 2*60*60);		
					return model;
				}
				else
				{
					return null;
				}
				 
			 //}
			 //return (ART.Model.M_ExamQuestion)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return GetList(0,strWhere,"");
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return GetList(0,"*",strWhere,filedOrder);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string FieldName,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" "+FieldName+" FROM ExamQuestion ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			if(filedOrder.Trim()!="")
			{
				strSql.Append(" order by " + filedOrder);
			}
			return SqlHelper.ExecDataSet(strSql.ToString());
		}
#endregion

      #region __________自定义方法__________
        public enum QuesttionTypeEnum
        {
            文本=0,
            单选=1,
            多选=2,
            填空=3
        }
      #endregion 
	}
}

